package com.yongcheon.sms_manager.data.sms_spam;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.widget.CursorAdapter;

import com.yongcheon.sms_manager.ui.sms_spam.list.SMSCursorAdapter;
import com.yongcheon.sms_manager.vo.SMSVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-19.
 */
public class SMSData extends AbstractSMSData {
    public static final String STR_CONTENT_URI_INBOX = "content://sms/inbox";
    public static final Uri CONTENT_URI_INBOX = Uri.parse(STR_CONTENT_URI_INBOX);


    public CursorAdapter getCurosrAdapter(Context context, SQLiteDatabase database){
//        Uri sentURI = Uri.parse("content://sms");
        Cursor c = context.getContentResolver().query(CONTENT_URI_INBOX, null, null, null, null);
        CursorAdapter adapter = new SMSCursorAdapter(context, c);
        return adapter;
    }

    public void insert(SMSVO vo, Context context, SQLiteDatabase database){
        Uri insertUri = Uri.parse("content://sms");
        ContentValues cv = new ContentValues();
//        cv.put("_id", vo.getSMS_id());
        cv.put("thread_id", vo.getThread_id());
        cv.put("address", vo.getAddress());
        cv.put("date", vo.getDate());
        cv.put("protocol", vo.getProtocol());
        cv.put("read", vo.getRead());
        cv.put("status", vo.getStatus());
        cv.put("type", vo.getType());
        cv.put("reply_path_present", vo.getReply_path_present());
        cv.put("subject", vo.getSubject());
        cv.put("body", vo.getBody());
        cv.put("service_center", vo.getService_center());
        cv.put("locked", vo.getLocked());
        cv.put("error_code", vo.getError_code());
        cv.put("seen", vo.getSeen());
        context.getContentResolver().insert(insertUri, cv);
    }

    public void delete(long id, Context context, SQLiteDatabase database){
        Uri SPAM_SMS_CONTENT_URI = Uri.parse("content://sms");
        Uri deleteUri = ContentUris.withAppendedId(SPAM_SMS_CONTENT_URI, id);
        String _id = String.valueOf(id);
        int result = context.getContentResolver().delete(deleteUri, "_id=?", new String[]{_id});
    }

    public ArrayList<SMSVO> select(String poneNum, Context context, SQLiteDatabase database){
//        Uri sentURI = Uri.parse("content://sms");
        Cursor c = context.getContentResolver().query(CONTENT_URI_INBOX, null,  "address=?", new String[]{poneNum}, "date desc");
        return super.getSMSListByNum(c);
    }

    public ArrayList<SMSVO> selectById(String id, Context context, SQLiteDatabase database){
//        Uri sentURI = Uri.parse("content://sms");
        Cursor c = context.getContentResolver().query(CONTENT_URI_INBOX, null,  "_id=?", new String[]{id}, null);
        return super.getSMSListByNum(c);
    }

    public void deleteByPhoneNum(String num, Context context, SQLiteDatabase database){
        Uri SPAM_SMS_CONTENT_URI = Uri.parse("content://sms");
        int result = context.getContentResolver().delete(SPAM_SMS_CONTENT_URI, "address=?", new String[]{num});
    }

    public void deleteById(String _id, Context context, SQLiteDatabase database){
        Uri SPAM_SMS_CONTENT_URI = Uri.parse("content://sms");
        int result = context.getContentResolver().delete(SPAM_SMS_CONTENT_URI, "_id=?", new String[]{_id});
    }

}
